import Vue from "vue";
import { getRandomNum, metalogin } from "@/api/module/home.js";
import loginModal from "@/components/loginModal/loginModal";
import {} from "@/common/wb3";
import Web3 from "web3";
export const loginMixins = {
  components: {
    loginModal, // 相关弹出框（绑定推荐人 登录及退出）
  },
  data() {
    return {
      loginModel: false, //登录
      singModel: false, //退出登录
      addressModel: false, //绑定推荐人
      Recommenderaddres: "", //推荐人地址
      text: "3fdgfsdfgdfhdhghdgh",
      user: "",
      userDddress: "",
      addressLink: " ",
      empower: "",
      isUSER: false,
    };
  },
  computed: {
    i18n() {
      return this.$t("home");
    },
  },
  actieved() {
    this.user = sessionStorage.getItem("user")
      ? sessionStorage.getItem("user")
      : "";
    this.text = this.user == "" ? "" : this.getCaption(this.user);
    console.log("当前用户钱包地址----", this.user);
    // if( this.user ==''){
    //   this.$refs.loginModalref.showLogin()
    // }
  },
  mounted() {
    this.user = sessionStorage.getItem("user")
      ? sessionStorage.getItem("user")
      : "";
    this.text = this.user == "" ? "" : this.getCaption(this.user);
    console.log("当前用户钱包地址----", this.user);
    // if( this.user ==''){
    //   this.$refs.loginModalref.showLogin()
    // }
    // this.$refs.loginModalref.showAddres();
  },
  methods: {
    toWallet() {
      console.log("打开钱包");
      this.$refs.loginModalref.showLogin();
    },
    singOutModel() {
      console.log("点击退出登录");
      this.$refs.loginModalref.showLoginOut();
    },

    //登录
    toSing() {
      console.log("点击了登录", this.user);
      var Web3 = require("web3");
      const web3 = new Web3(window.ethereum);
      let that = this;
      if (that.user == "") {
        if (typeof window.ethereum === "undefined") {
        } else {
          ethereum
            .enable()
            .catch(function (reason) {})
            .then(async function (accounts) {
              const add = ethereum.enable().then((res) => {});
              const account = accounts[0];
              uni.setStorageSync("account", account);
              that.loginModel = false;
              // that.$i18n.locale = "CN";
              that.user = account;

              console.log("USER已存储-------", that.user);
              sessionStorage.setItem("userDddress", account);
              sessionStorage.setItem("user", account);
              that.text = account == "" ? "登录" : that.getCaption(account);
              //  location.reload()
              that.isBindReferrer();
            });
        }
      } else {
        //that.showmodal = true;
      }
    },
    init() {
      this.user = sessionStorage.getItem("user")
        ? sessionStorage.getItem("user")
        : "";
      console.log("更新user值---&&&&&&&&", this.user);
    },
    getCaption(strings) {
      var strLength = strings.length;
      var shortName = strings.substring(0, 5);
      var shortName2 = strings.substring(strLength - 5, strLength);
      let str = `${shortName}....${shortName2}`;
      return str;
    },

    singOut() {
      console.log("确定退出登录");
      localStorage.clear();
      sessionStorage.clear();
      this.singModel = false;
      location.reload();
    },

    // 查询用户是否已绑定推荐人，返回true代表已绑定，false代表未绑定
    async isBindReferrer() {
      console.log("this.user1------", this.user);
      let res = await xxx.methods.queryIsBindRecommender(this.user).call();
      console.log("是否绑定", res);
      //true，已绑定，false，未绑定
      if (res) {
        location.reload();
      } else {
        this.Recommenderaddres = window.location.href.split("id=")[1];
        console.log("Recommenderaddres", this.Recommenderaddres);
        this.addressModel = true;
        this.$refs.loginModalref.showAddres();
      }
    },
    //绑定推荐人
    subAddress() {
      this.user = sessionStorage.getItem("user")
        ? sessionStorage.getItem("user")
        : "";
      console.log("this.user2------", this.user);
      console.log("推荐人地址确认", this.Recommenderaddres);
      if (this.Recommenderaddres !== "") {
        console.log("提交推荐人");
        this.bind();
      } else {
        uni.showToast({
          //title: '请输入推荐人地址',
          title: this.$t("home.h69"),
          icon: "none",
        });
      }
    },
    async bind() {
      this.user = sessionStorage.getItem("user")
        ? sessionStorage.getItem("user")
        : "";
      console.log("参数3---------", this.user);
      try {
        let res = await Controller_contracts.methods
          .checkBindRecommender(this.user, this.Recommenderaddres)
          .call();
        console.log("检查可绑定推荐人", res);
        switch (res) {
          case "0":
            this.setbind();
            break;
          case "1":
            uni.showToast({
              icon: "none",
              title: this.$t("home.h94"),
              //title:'已绑定过推荐人',
            });
            break;
          case "2":
            uni.showToast({
              icon: "none",
              title: this.$t("home.h95"),
              // title:'綁定的推荐人地址无效',
            });
            break;
          default:
            break;
        }
      } catch (error) {
        console.log("綁定的推荐人地址无效-----");
        uni.showToast({
          icon: "none",
          title: this.$t("home.h95"),
          // title:'綁定的推荐人地址无效',
        });
      }
    },
    //绑定推荐人
    async setbind() {
      uni.showLoading({
        // title: '绑定中',
        title: this.$t("home.h93"),
        mask: true,
      });
      this.user = sessionStorage.getItem("user")
        ? sessionStorage.getItem("user")
        : "";
      console.log("this.user", this.user);
      console.log("this.Recommenderaddres", this.Recommenderaddres);
      try {
        let result = await Controller_contracts.methods
          .bindRecommender(this.Recommenderaddres)
          .send({ from: this.user, gasPrice: "50000000000", gas: "100000" });
        console.log("绑定推荐人成功-----", result);
        this.addressModel = false;
        uni.hideLoading();
        uni.showToast({
          // title:'绑定成功'
          title: this.$t("home.h96"),
        });
      } catch (error) {
        console.log("绑定推荐人失败-----", error);
        uni.hideLoading();
        uni.showToast({
          icon: "none",
          // title:'绑定失败'
          title: this.$t("home.h97"),
        });
      }
    },
  },
};
